package com.ict.ms.artificial.service.api.impl;




import mqutils.MQTopicPublish;
import mqutils.ProperUtilsMQ;
import org.springframework.stereotype.Service;

import com.ict.ms.artificial.facade.api.IAttributeService;
import com.ict.ms.artificial.facade.model.Attribute;
import com.ict.ms.artificial.facade.model.AttributeDto;
import com.ict.ms.artificial.service.dao.IAttributeDao;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by StevenH on 2017/10/19.
 * 属性修改
 */
@Service("attributeService")
public class AttributeService  implements  IAttributeService{

    @Resource
    IAttributeDao attributeDao;

    /**
     * 读取属性修改表
     * @return
     */
    public List<Attribute> getAttributes(){
        return attributeDao.getAttributes();
    }

    /**
     * 读取单条属性修改
     * @return 值如果修改,相关属性为1,没有修改,为0
     */
    public AttributeDto getAttributeChange(int id){
        Attribute attribute=attributeDao.getAttributeChange(id);
        return changeDto(attribute);
    }

    /**
     * 判断单条属性是否被修改
     * @param attribute 单条属性信息
     * @return 属性修改的dto 属性0没有修改,属性1修改.
     */
    public AttributeDto changeDto(Attribute attribute){
        AttributeDto dto=new AttributeDto();
        dto.setTargetid(attribute.getTargetid());
        if(attribute.getType_new()==attribute.getType_old()){
            dto.setType(0);
        }else {
            dto.setType(1);
        }
        if(attribute.getCountry_new().equals(attribute.getCountry_old())){
            dto.setCountry(0);
        }else {
            dto.setCountry(1);
        }
        if(attribute.getName_new().equals(attribute.getName_old())){
            dto.setName(0);
        }else {
            dto.setName(1);
        }
        return dto;
    }

    /**
     * 读取所有属性修改
     * @return 值如果修改,相关属性为1,没有修改,为0
     */
    public List<AttributeDto> getAttributeChangeList(){
        List<Attribute> attributes=attributeDao.getAttributes();
        List<AttributeDto> dtos=new ArrayList<AttributeDto>();
        for(Attribute attribute:attributes){
            AttributeDto dto= changeDto(attribute);
            dtos.add(dto);
        }
        return dtos;
    }
    /**
     * 撤销属性修改
     * @param targetId 被撤销的ID
     * @return
     */
    public boolean revocation(int targetId){
        //发送PB
        team.iOceanPlus.PB.Command.PBCommand.Builder imtatgetPB = team.iOceanPlus.PB.Command.PBCommand.newBuilder();

        team.iOceanPlus.PB.Command.PBCommandCancelUpdateTargetProperty.Builder PBCommandCancelUpdateTargetProperty = team.iOceanPlus.PB.Command.PBCommandCancelUpdateTargetProperty.newBuilder();
        PBCommandCancelUpdateTargetProperty.setTargetID(targetId);

        imtatgetPB.addListCommandCancelUpdateTargetProperty(PBCommandCancelUpdateTargetProperty);
        ProperUtilsMQ properUtilsMQ=new ProperUtilsMQ("xgs","Command.CancelUpdateTargetProperty","");

        MQTopicPublish mqTopicPublish = new MQTopicPublish(properUtilsMQ.getHost(), properUtilsMQ.getUser(), properUtilsMQ.getPassWord(),
                properUtilsMQ.getExchange());

        if (mqTopicPublish.BaseConnection(properUtilsMQ.getPort()) != 0) {
            System.out.println("连接RabbiqMQ失败");
            return false;
        }

        //PB打包成byte[]
        team.iOceanPlus.PB.Command.PBCommand bui = imtatgetPB.build();
        byte[] result = bui.toByteArray();

        //调用RabbitMQ发布到指定的队列
        if (!mqTopicPublish.Publish(properUtilsMQ.getRoutingKey(), result)) {
            System.out.println("发送RabbitMQ消息失败");
            return false;
        }

        //在数据库中删掉
        try {
            attributeDao.revocation(targetId);
            return true;
        }catch (Exception e){
            return false;
        }
    }

}
